package com.dachuang.canjiren.config;

import com.dachuang.canjiren.entity.Permission;
import com.dachuang.canjiren.entity.Role;
import com.dachuang.canjiren.entity.User;
import com.dachuang.canjiren.service.RegisterService;
import com.dachuang.canjiren.service.UserService;
import org.apache.ibatis.annotations.Param;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.jasypt.encryption.StringEncryptor;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;

/**
 * 自定义realm
 */
public class CustomRealm extends AuthorizingRealm {

    @Autowired
    private UserService userService;

    @Autowired
    private StringEncryptor stringEncryptor;

    @Autowired
    private RegisterService registerService;
    /**
     * 进行权限校验的时候回调用
     * @param principals
     * @return
     */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
        System.out.println("授权 doGetAuthorizationInfo");
        String username = (String)principals.getPrimaryPrincipal();

        User user = userService.FindAllInfoByUserId(username);

        List<String> stringRoleList = new ArrayList<>();
        List<String> stringPermissionList = new ArrayList<>();


        List<Role> roleList = user.getRoleList();

        for(Role role : roleList){
            stringRoleList.add(role.getRoleName());

            List<Permission> JurisdictionList = role.getJurisdictionList();

            for(Permission p: JurisdictionList){
                if(p!=null){
                    stringPermissionList.add(p.getPermission());
                }
            }

        }

        SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo();
        simpleAuthorizationInfo.addRoles(stringRoleList);
        simpleAuthorizationInfo.addStringPermissions(stringPermissionList);

        return simpleAuthorizationInfo;
    }









    /**
     * 用户登录的时候会调用
     * @param token
     * @return
     * @throws AuthenticationException
     */


    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {

        System.out.println("认证 doGetAuthenticationInfo");

        //从token获取用户信息，token代表用户输入
        String username = (String)token.getPrincipal();

        User user = userService.FindAllInfoByUserId(username);
        User user1 = registerService.FindAllInfoByUserEmail(username);

        if(user == null || "".equals(user)){
            user = user1;
        }

        //取密码
        String pwd1 = user.getPassword();
        String pwd = stringEncryptor.decrypt(pwd1);
        if(pwd == null || "".equals(pwd)){

        }

        return new SimpleAuthenticationInfo(username, pwd, this.getClass().getName());
    }
}
